package com.gec.system.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gec.model.entity.SysHouse;
import com.gec.model.entity.SysRepair;
import com.gec.model.entity.SysRepair;
import com.gec.model.vo.SysHouseQueryVo;
import com.gec.model.vo.SysRepairVo;
import com.gec.system.service.SysRepairService;
import com.gec.system.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 维修表  用于记录用户报修信息 前端控制器
 * </p>
 *
 * @author lmd
 * @since 2023-12-08
 */
@Api(tags = "维修管理控制器")
@RestController
@RequestMapping("/admin/system/sysRepair")
public class SysRepairController {

    @Autowired
    private SysRepairService sysRepairService;

    @ApiOperation("查询所有维修信息")
    @GetMapping("/findAllRepair")
    public Result finAllRepair(){

        List<SysRepair> list = this.sysRepairService.list();
        return Result.ok(list);
    }

    //根据维修状态查询维修信息
    @ApiOperation("根据维修状态查询维修信息")
    @GetMapping("/findAllRepairByStatus/{status}")
    public Result findAllRepairByStatus(@PathVariable Integer status){

        List<SysRepair> list = this.sysRepairService.findAllRepairByStatus(status);
        return Result.ok(list);
    }


    @ApiOperation("分类查询")
    @GetMapping("/{page}/{limit}")
    public Result selectRepairPageVo(@PathVariable Long page,
                                    @PathVariable Long limit,
                                    SysRepairVo sysRepairVo
    ){
        IPage<SysRepair> iPage =new Page<>(page,limit);

        System.out.println("iPage条件："+iPage);

        iPage=this.sysRepairService.selectPage(iPage,sysRepairVo);

        System.out.println("控制层打印楼栋信息："+iPage);
        return Result.ok(iPage);
    }

    //    新增
    @ApiOperation("新增维修信息")
    @PostMapping("/addRepair")
    public Result addRepair(@RequestBody SysRepair sysRepair) {
        boolean save = this.sysRepairService.save(sysRepair);
        return save ? Result.ok() : Result.fail();

    }

    // 修改
    //1.根据id 去得到当前分类
    @ApiOperation("根据id获取维修信息")
    @GetMapping("/findRepairById/{id}")
    public Result findRepairById(@PathVariable Long id)
    {
        SysRepair sysRepair = this.sysRepairService.getById(id);
        return Result.ok(sysRepair);
    }

    // 实现修改
    @ApiOperation("修改维修信息")
    @PostMapping("/updateRepair")
    public Result updateRepair(@RequestBody SysRepair sysRepair)
    {
        boolean b = this.sysRepairService.updateById(sysRepair);
        return b ? Result.ok() : Result.fail();

    }

    @PreAuthorize("hasAuthority('btn.sysRepair.remove')")
    //    根据单个id 删除数据
    @ApiOperation("根据id删除维修信息")
    @DeleteMapping("/removeRepairById/{id}")
    public Result removeRepairById(@PathVariable Long id){

        boolean b = this.sysRepairService.removeById(id);
        return b ? Result.ok() : Result.fail();

    }

    // 批量删除
    @ApiOperation("批量删除维修信息")
    @DeleteMapping("/removeRepairByIds")
    public Result removeRepairByIds(@RequestBody List<Long> ids)
    {
        boolean b = this.sysRepairService.removeByIds(ids);

        return b ? Result.ok() : Result.fail();

    }


    // 更改维修状态
    @ApiOperation("更改维修状态")
    @GetMapping("/updateRepairStatus/{id}/{status}")
    public Result updateRepairStatus(@PathVariable Long id,
                                      @PathVariable Integer status)
    {
        this.sysRepairService.updateRepairStatus(id,status);
        return Result.ok();
    }

}

